
function [p q] = Chorin_Projection(dt)
global U  h  g  p  q r  nt dx x h1 itermax p_global;

dx1=1/dx;
dx2 = dx1*dx1;
dt2= 1/dt;
beta=0.5;
%---- Construct the spatial Discretization Matrix------%
A = zeros(x,x);
coeff1= U*0.5*dx1;

A(1,x) = -coeff1;
A(1,2) =  coeff1;
for k=2:x-1
    A(k,k-1) = -coeff1;
    A(k,k+1) =  coeff1;
end

A(x,x-1) = -coeff1;
A(x,1)   =  coeff1;

L=zeros(2*x,2*x);
L(x+1:2*x,x+1:2*x)=A;
L(1:x,1:x)=A;

coeff2 = h*dx2;

L(1,2*x)   = coeff2;
L(1,x+2)   = coeff2;
L(x,x+1)   = coeff2;
L(x,2*x-1) = coeff2;
for k=1:x
    L(k,x+k)= -2*coeff2;
    L(x+k,k)=g;
end

for k=2:x-1
    L(k,x+k-1) = coeff2;
    L(k,x+k+1)= coeff2;
end

b=h*h/3*dx2;
C=zeros(2*x,x);
C(1,x)= b;
C(1,2)= b;
C(1,1)= -2*b;
for k=2:x-1
    C(k,k-1)=b;
    C(k,k+1) =b;
    C (k,k) = -2*b;
end
C(x,x-1)= b;
C(x,1)= b;
C(x,x)= -2*b;

D = C(1:x,1:x);


n = -2/15*h*h*h*dx2;
coeff3 = h/3;
E=zeros(x,x);
E(1,x)= n;
E(1,2)= n ;
E(1,1)= -2*n+coeff3;

for k=2:x-1
    E(k,k-1)=n;
    E(k,k+1) =n;
    E (k,k) = -2*n+coeff3;
end

E(x,x-1)= n;
E(x,1)= n;
E(x,x)= -2*n+coeff3;


I =eye(2*x,2*x);
r= zeros(x,1);

sol(1:x,1) =p;
sol(x+1:2*x,1) =q;
%sol_new=sol;
p_global(:,1)=p;
for n=2:nt+1; 
    
    rhs = (dt2*I - (1-beta)*L)*sol;
    sol_new=(dt2*I+beta*L)\rhs;
    
    r=R_Solve(sol_new(x+1:2*x));
    sol = sol_new + dt*C*r;

    p = sol(1:x,1);
    q= sol(x+1:2*x,1);    
    p_global(:,n)=p;

    if rem(n,100)==0
    %    refreshdata(h1,'caller') % Evaluate p in the function workspace
    %   drawnow
    end

end
display('Completed Successfully');